{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. Resampling Methods – Conceptual\n",
    "\n",
    "Excercises from **Chapter 5* of [An Introduction to Statistical Learning](http://www-bcf.usc.edu/~gareth/ISL/) by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Using basic statistical properties of the variance, as well as single- variable calculus, derive (5.6). In other words, prove that α given by (5.6) does indeed minimize Var(αX + (1 − α)Y ).\n",
    "\n",
    "![5_1.jpg](./images/5_1.jpg)\n",
    "\n",
    "\n",
    "## 2. We will now derive the probability that a given observation is part of a bootstrap sample. Suppose that we obtain a bootstrap sample from a set of n observations.\n",
    "\n",
    "- (a) What is the probability that the first bootstrap observation is not the jth observation from the original sample? Justify your answer.\n",
    "- (b) What is the probability that the second bootstrap observation is not the jth observation from the original sample?\n",
    "- (c) Argue that the probability that the jth observation is not in the bootstrap sample is (1 − 1/n)n.\n",
    "- (d) When n = 5, what is the probability that the jth observation is in the bootstrap sample?\n",
    "- (e) When n = 100, what is the probability that the jth observation is in the bootstrap sample?\n",
    "- (f) When n = 10, 000, what is the probability that the jth observa- tion is in the bootstrap sample?\n",
    "\n",
    "![5_2.jpg](./images/5_2.jpg)\n",
    "\n",
    "### 2(g) Create a plot that displays, for each integer value of n from 1 to 100,000, the probability that the jth observation is in the bootstrap sample. Comment on what you observe."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0,0.5,'probability')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEKCAYAAAAB0GKPA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AkSZ0YGJKkTgwMSVInBoYkqRMDQ5LUiYEhSerEwJAkdfL/Ab5Bi83pB09DAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def prob_j_in_sample(n):\n",
    "    return 1 - (1 - 1/n)**n\n",
    "\n",
    "x = np.arange(1, 100000)\n",
    "y = np.array([prob_j_in_sample(n) for n in x])\n",
    "\n",
    "ax = sns.lineplot(x=x, y=prob_j_in_sample(x))\n",
    "plt.xlabel('n')\n",
    "plt.ylabel('probability')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2(h) We will now investigate numerically the probability that a bootstrap sample of size n = 100 contains the jth observation. Here j = 4. We repeatedly create bootstrap samples, and each time we record whether or not the fourth observation is contained in the bootstrap sample."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6353635363536354"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "store = [] \n",
    "for i in np.arange(1, 10000):\n",
    "    store += [np.sum((np.random.randint(low=1, high=101, size=100) == 4)) > 0]\n",
    "\n",
    "np.mean(store)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Comment**\n",
    "\n",
    "The result observed from a numerical approach above is simimilar to to our probabilistic estimation for a sample size of 100 which was P = 0.634.\n",
    "\n",
    "It is interesting to note that there is a suprisingly high level of variability between results given that results are averaged over 10000 tests. This can be observed by running the above cell multiple time (note that no random seed is set)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. We now review k-fold cross-validation.\n",
    "\n",
    "### (a) Explain how k-fold cross-validation is implemented.\n",
    "\n",
    "In k-fold cross validation k independant samples are taken from the set of all available observations each of size, $1 - (\\frac{n}{k})$\n",
    "\n",
    "The model is then fitted to each of these training samples, and then tested on the observations that were excluded from that sample. This produces k error scores which are then averaged to produce the final cross-validation score.\n",
    "\n",
    "Note that the proportion of observations that are included in each training increases increases with k.\n",
    "\n",
    "### (b) What are the advantages and disadvantages of k-fold cross- validation relative to:\n",
    "\n",
    "### - i. The validation set approach?\n",
    "\n",
    "When $k>2$, cross-validation provides a larger training set than the validation set approach. This means that there is less bias in the training setting. This means crossvalidation can produce more accurate estimates for more flexible models that benefit from a larger number of observations in the training set.\n",
    "\n",
    "Cross-validation results will exhibit more variability than the validation set approach. The approach is also more computationally expensive because the model must be fitted and tested for each fold in k.\n",
    "\n",
    "### - ii. LOOCV?\n",
    "\n",
    "Cross-validation for k<n provides less variance in accuracy scores than LOOCV. It is also less computationally expensive in most settings (assuming that a linear regression model isn't used and a certain mathematical shortcut implemented for LOOCV).\n",
    "\n",
    "LOOCV provides the maximum proportion of the original observations for training, which could provide an improved accuracy estimate if the model is limited by the size of training set available in k-fold cross validation (for some small value of k). LOOCV can also be more computationally efficient for linear regression assuming that the afformentioned mathematical shortcut is implemented.\n",
    "\n",
    "\n",
    "## 4. Suppose that we use some statistical learning method to make a prediction for the response Y for a particular value of the predictor X. Carefully describe how we might estimate the standard deviation of our prediction.\n",
    "\n",
    "**INCORRECT: Need to revisit**\n",
    "\n",
    "\n",
    "Given n observations of X sampled from the population, we use our model to make the corresponding predictions for the response Y. We can then estimate the standard deviation of the the responses using the formula:\n",
    "\n",
    "$\\hat{σ} = \\sqrt{\\frac{1}{n - 1}\\sum_{i=1}^{n}{(\\hat{y_i} - \\bar{y})^2}}$\n",
    "\n",
    "The accuracy of this estimate is limited by its variability. \n",
    "\n",
    "We can use the bootstrap method to improve the accuarcy of this estimate. We randomly select n observations from the original dataset to produce a boostrap dataset $X^{*1}$, the sampling is done with replacement. We can use $X^{*1}$ to produce a new bootstrap estimate $\\hat{σ}^{*1}$. We repeat this procedure $B$ times for some large value of $B$ to produce $B$ different bootstrap datasets, $X^{*1}$, $X^{*2}$,..., $X^{*B}$, and $B$ resultant $σ$ estimates, $\\hat{σ}^{*1}$, $\\hat{σ}^{*2}$,...,$\\hat{σ}^{*B}$. The estimate $\\hat{σ}$ is then:\n",
    "\n",
    "$\\hat{σ} = E[\\hat{σ}^{*B}]$\n",
    "\n",
    "For large values $B$ should provide an improved estimate $\\hat{σ}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
